//
//  YCDeviceMotionPushViewController.m
//  DemoDeviceMotion
//
//  Created by 唐可雄 on 16/8/31.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "YCDeviceMotionPushViewController.h"

#import <CoreMotion/CoreMotion.h>

@interface YCDeviceMotionPushViewController ()

/// 传感器管理器
@property (strong, nonatomic) CMMotionManager *manager;

@end

@implementation YCDeviceMotionPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.manager.isDeviceMotionAvailable) {
        // 加速计和陀螺仪复合数据可用
        [self.manager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
            // 当采样到加速计和陀螺仪复合数据信息时就会执行
            if (error) {
                return;
            }
            
            self.textView.text = [self.textView.text stringByAppendingString:[NSString stringWithFormat:@"%@\n", motion]];
        }];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"加速计和陀螺仪复合数据不可用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Setter and Getter

- (CMMotionManager *)manager {
    if (_manager) {
        _manager = [[CMMotionManager alloc] init];
        
        // 1秒采集1次
        _manager.deviceMotionUpdateInterval = 1./1.;
    }
    
    return _manager;
}

#pragma mark - IBAction

- (IBAction)clearBtnDidClick:(id)sender {
    self.textView.text = nil;
}

@end
